package com.atguigu.queue;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        boolean empty1 = arrayQueue.isEmpty();
        System.out.println("empty1 = " + empty1);
        boolean full1 = arrayQueue.isFull();
        System.out.println("full1 = " + full1);
        arrayQueue.addEle(2);
        arrayQueue.addEle(1);
        arrayQueue.addEle(4);
        arrayQueue.show();
        boolean empty = arrayQueue.isEmpty();
        System.out.println("empty = " + empty);
        boolean full = arrayQueue.isFull();
        System.out.println("full = " + full);
        int ele = arrayQueue.getEle();
        System.out.println("ele = " + ele);
        boolean full3 = arrayQueue.isFull();
        System.out.println("full3 = " + full3);
        int i = arrayQueue.headQueue();
        System.out.println("i = " + i);

    }
}

//使用数组和模拟队列
class ArrayQueue{
     private int front;//表示队列的头部
     private int rear;//队列的尾部
     private int maxSize;//数组的大小
     private int[] arr;//创建数组

     //构造器
    public ArrayQueue(int maxSize){
        this.front=-1;//指向队列的第一个数据
        this.rear=-1;//指向队列的最后一个元素的后一个位置。空出一个空间作为约定
        this.maxSize=maxSize;
        arr=new int[maxSize];
    }
    //判断队列是否满了
    public boolean isFull(){
        return rear==maxSize-1;
    }
    //队列是否为空
    public boolean isEmpty(){
        return rear==front;
    }
    //添加数据
    public void addEle(int i){
        if(isFull()){
            System.out.println("队列满了");
            return;
        }

        arr[++ rear]=i;
    }
    //取出数据
    public int getEle(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据可以取出");
        }
        return arr[++front];
    }
    //遍历数据
    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据可以遍历");
        }
        for (int i=0;i<arr.length;i++) System.out.printf("arr[%d]=%d\n", i, arr[i]);
    }
    //显式头元素
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据可以显式");
        }
        return arr[++front];
    }
}


class CircleArray{
    private int front;//表示队列的头部 front=0
    private int rear;//队列的尾部的后一个位置
    private int maxSize;//数组的大小
    private int[] arr;//创建数组
    //构造器
    public CircleArray(int maxSize){
        this.front=0;//指向队列的第一个数据
        this.rear=0;//指向队列的最后一个元素的后一个位置。空出一个空间作为约定
        this.maxSize=maxSize;
        arr=new int[maxSize];
    }
    //判断队列是否满了
    public boolean isFull(){
        return (rear+1)%maxSize==front;
    }
    //队列是否为空
    public boolean isEmpty(){
        return rear==front;
    }
    //添加数据
    public void addEle(int i){
        if(isFull()){
            System.out.println("队列满了");
            return;
        }
        arr[rear]=i;
        rear=(rear+1)%maxSize;
    }
    //取出数据
    public int getEle(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据可以取出");
        }
        int temp=front;
        front = (front + 1) % maxSize;
        return arr[temp];
    }
    //遍历数据
    public void show(){
        if(isEmpty()){
            throw new RuntimeException("队列为空，没有数据可以遍历");
        }
        //从front开始遍历
        for (int i=front;i<front+size();i++) System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
    }

    public int size(){
        return (rear+maxSize-front)%maxSize;
    }


}